﻿using Architecture.Domain.Dto.MenuDto;
using Architecture.Domain.Dto.MenuRoleDto;
using Architecture.Repository.Impl;
using Architecture.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Domain.Service.MenuRoleServices
{
    /// <summary>
    /// 菜单角色服务
    /// </summary>
    public class MenuRoleService : IMenuRoleService
    {
        /// <summary>
        /// 用户信息仓储
        /// </summary>
        protected readonly IUserInfoRepository _userInfoRepository;

        /// <summary>
        ///  用户信息角色服务
        /// </summary>
        protected readonly IUserInfoRoleRepository _userInfoRoleRepository;

        /// <summary>
        /// 角色仓储
        /// </summary>
        protected readonly IRoleRepository _roleRepository;

        /// <summary>
        /// 菜单仓储
        /// </summary>
        protected readonly IMenuRepository _menuRepository;

        /// <summary>
        /// 菜单角色仓储
        /// </summary>
        private readonly IMenuRoleRepository _menuRoleRepository;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userInfoRepository">用户信息仓储</param>
        /// <param name="userInfoRoleRepository">用户信息角色服务</param>
        /// <param name="roleRepository">角色仓储</param>
        /// <param name="menuRepository">菜单仓储</param>
        /// <param name="menuRoleRepository">菜单角色仓储</param>
        public MenuRoleService(IUserInfoRepository userInfoRepository, IUserInfoRoleRepository userInfoRoleRepository, IRoleRepository roleRepository, IMenuRepository menuRepository, IMenuRoleRepository menuRoleRepository)
        {
            _userInfoRepository = userInfoRepository;
            _userInfoRoleRepository = userInfoRoleRepository;
            _roleRepository = roleRepository;
            _menuRepository = menuRepository;
            _menuRoleRepository = menuRoleRepository;
        }

        /// <summary>
        /// 添加菜单角色
        /// </summary>
        /// <param name="addMenuRole">用户角色信息</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> AddMenuRole(AddMenuRole addMenuRole)
        {
            var menuRole = new MenuRole();
            int key = 0;
            foreach (var item in addMenuRole.ids)
            {
                if (item != 0)
                {
                    var result = await _menuRoleRepository.GetAsync(x => x.MID == item && x.RID == addMenuRole.rid);
                    if (result.Count == 0)
                    {
                        menuRole.MenuRoleId = 0;
                        menuRole.MID = item;
                        menuRole.RID = addMenuRole.rid;
                        key += await _menuRoleRepository.AddAsync(menuRole);
                    }
                }
            }
            return key;
        }

        /// <summary>
        /// 反填菜单角色MId列表
        /// </summary>
        /// <returns>返回菜单角色MId列表</returns>
        public async Task<List<MenuRoleGetIdDto>> GetMenuRoleMId(string roleId)
        {
            var list = (from a in await _menuRepository.GetAllAsync()
                        join b in await _menuRoleRepository.GetAllAsync()
                        on a.MenuId equals b.MID
                        join c in await _roleRepository.GetAllAsync()
                        on b.RID equals c.RoleId
                        where c.RoleId.ToString() == roleId && !a.IsDeleted
                        select new MenuRoleGetIdDto
                        {
                            id = b.MID
                        });
            return list.ToList();
        }
    }
}
